#include "stdio.h"
#include "string.h"
#include "Includes.h"
#include "APP_Main.h"
#include "SVS_Para.h"
#include "SVS_Station.h"
#include "DEV_Station.h"

extern char *strGunDesc[];


//桩参数
U8 STAType=1;//0.直流桩，1.交流桩
U8 STANo[STANO_LENGTH]={0x43,0x20,0x50,0x00,0x42,0x40,0x01};//桩编号

//枪参数
U8 GunNum=1;
U8 GunReturn=0x02;//未知，0x00 否 0x01 是 0x02 未知
U8 GunPlugin=0x00;//0x00 否 0x01 是
U8 GunTemp=0x00;//枪线温度
U8 GunCode[GUNCODE_LENGTH]={0};
U8 CardDoSwip=0;

//卡参数
PHYCARDID_UN PhyCardID={.ID1=0,.ID2=0xAB11F45E};

GUN_STA_E g_enGunState=GUN_PLUG_IDLE;

//桩参数初始化
int StaInit(void)
{
    int err;
    unsigned int length=sizeof(STANo);
    
    err=AppParaRead(APS_PARA_MODULE_DEV_STATION, STA_PARA_STANO, STANo, &length);
    if(err !=0)
    {
        printf("station don't have station code number!!!\r\n");
    }
    length=sizeof(BFNo);
    err=AppParaRead(APS_PARA_MODULE_DEV_STATION, STA_PARA_BFNO, BFNo, &length);
    if(err !=0)
    {
        printf("station don't have BFN number!!!\r\n");
    }
    
    return 0;
}

//返回桩编码地址
U8 *StaNoGet(void)
{
    return STANo;
}

//设置桩编码
int StaNoSet(U8 * data, U8 len)
{
    int err=0;
    err=AppParaWrite(APS_PARA_MODULE_DEV_STATION, STA_PARA_STANO, data, len);
    if(err !=0)
    {
        printf("write station code number err:%d!!!\r\n",err);
        return err;
    }
    memcpy(STANo,data,sizeof(STANo));
    return 0;
}

//返回交易流水号地址
U8 *BFNoGet(void)
{
    return BFNo;
}

//设置交易流水号
int BFNoSet(U8 * data, U8 len)
{
    int err=0;
    err=AppParaWrite(APS_PARA_MODULE_DEV_STATION, STA_PARA_BFNO, data, len);
    if(err !=0)
    {
        printf("write station BFN number err:%d!!!\r\n",err);
        return err;
    }
    memcpy(BFNo,data,sizeof(BFNo));
    return 0;
}

int GunIsReturn(void)
{
    return GunReturn;
}

int GunIsPlugin(void)
{
    if(GUN_PLUG_IDLE < g_enGunState && g_enGunState <= GUN_PLUG_ERR)
        GunPlugin=1;
    else
        GunPlugin=0;
    return GunPlugin;
}

//车充电已准备就绪
int CarIsReady(void)
{
    if(GUN_PLUG_READY == g_enGunState)
        return 1;
    else
        return 0;
}
//车是否闭合充电回路S2继电器,返回值0.S2断开,1.S2闭合,2.异常
int CarIsS2TurnOn(void)
{
    if(GUN_PLUG_CONT == g_enGunState)
        return 0;
    else if(GUN_PLUG_READY == g_enGunState)
        return 1;
    return 2;
}

//充电过程中判断车是否已充满电
int CarIsFull(void)
{
    if(StationStateGet()==STA_STATE_CHG && 
        ChgSubStateGet()==STA_SUB_CHGING_STATE_CHGING && 
        GunStateGet() == GUN_PLUG_CONT)
        return 1;
    else
        return 0;
}

int GunTempGet(void)
{
    return GunTemp;
}

//返回值枪码长度
int GunCodeGet(U8 *data, U8 dlen)
{
    memcpy(data,GunCode,GUNCODE_LENGTH);
    return GUNCODE_LENGTH;
}

GUN_STA_E GunStateGet(void)
{
    return g_enGunState;
}

void MakeCardSwipAction(void)
{
    printf("====>Do swip card action!!!\r\n");
    CardDoSwip=1;
}

//调用清零
U8 IsCardSwipAction(void)
{
    if(CardDoSwip==1)
    {
        CardDoSwip=0;
        return 1;
    }
    return 0;
}

void StaLOOP(void)
{
    static GUN_STA_E lastGunState=GUN_PLUG_IDLE;

    //调试
    //g_enGunState=get_gun_sta();
    
    //枪状态变化
    if(lastGunState != GunStateGet())
    {
        printf("Gun state is: %s\r\n",strGunDesc[GunStateGet()]);
        lastGunState=GunStateGet();
        //上报监控状态
        CommMonitorRpt(NULL);
    }
}


